﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpExt.Collections
{
    public class Properties : Dictionary<String, String>
    {
        #region Constructor

        public Properties()
        {
        }

        public Properties(Properties props)
        {
            this.Union(props);
        }

        #endregion

        #region Properties

        public String[] Names
        {
            get { return this.Keys.ToArray(); }
        }

        #endregion

        #region Read

        public bool HasProperty(String name)
        {
            return this.ContainsKey(name);
        }

        public String GetProperty(String name)
        {
            return this[name];
        }

        public bool GetProperty(String name, bool defaultValue)
        {
            String value;
            if (this.TryGetValue(name, out value))
            {
                return Boolean.Parse(value);
            }
            else
            {
                return defaultValue;
            }
        }

        public int GetProperty(String name, int defaultValue)
        {
            String value;
            if (this.TryGetValue(name, out value))
            {
                return int.Parse(value);
            }
            else
            {
                return defaultValue;
            }
        }

        public double GetProperty(String name, double defaultValue)
        {
            String value;
            if (this.TryGetValue(name, out value))
            {
                return double.Parse(value);
            }
            else
            {
                return defaultValue;
            }
        }

        public String GetProperty(String name, String defaultValue)
        {
            String value;
            if (this.TryGetValue(name, out value))
            {
                return value;
            }
            else
            {
                return defaultValue;
            }
        }

        #endregion

        #region Write

        public void SetProperty(String name, bool value)
        {
            this[name] = value.ToString();
        }

        public void SetProperty(String name, int value)
        {
            this[name] = value.ToString();
        }

        public void SetProperty(String name, double value)
        {
            this[name] = value.ToString();
        }

        public void SetProperty(String name, String value)
        {
            this[name] = value;
        }

        #endregion
    }
}
